﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Util
{
    public class UserAgentHelper
    {
        private static List<string> UserAgents= new List<string>();
        private UserAgentHelper()
        {

            string path = AppDomain.CurrentDomain.BaseDirectory + "file/useragent.txt";
            if (File.Exists(path)) 
            {
                UserAgents.AddRange(File.ReadAllLines(path));
            }


        }

        public static string GetUserAgent(string path=null)
        {
            if (string.IsNullOrWhiteSpace(path) && UserAgents.Count > 0)
            {
                Random random = new Random();
                int index = random.Next(0, UserAgents.Count);
                return UserAgents[index];
            } else if (!string.IsNullOrWhiteSpace(path))
            {
                UserAgents.AddRange(File.ReadAllLines(path));
                return GetUserAgent();
            }

            return null;
        }

    }
}
